/*
	Copyright (C) 2011 Daniel C. Dennison (dandennison84@gmail.com).  All rights reserved.
	This file is licensed under the EPL 1.0 (Eclipse Public License 1.0)
	A copy of the license can be found at http://www.eclipse.org/legal/epl-v10.html
*/

package com.gmail.dandennison84.accounting

import org.junit.Test
import org.junit.Before

class JournalTest {
	Date d1, d2
	Account cash, shortTermDebt, equipment
	JournalEntryAmount credit1, credit2, credit3, credit4, debit1, debit2
	JournalEntry je1, je2
	Journal j

	@Before
	void before() {
		d1 = new Date(1)
		je1 = new JournalEntry(dateTime:d1)
		cash = new Account(name:'Cash', accountType:AccountType.ASSETS, closingType:ClosingType.PERMANENT, balanceType:BalanceType.DEBIT,
			cashFlowStatementType:CashFlowStatementType.OPERATING_ACTIVITIES)
		shortTermDebt = new Account(name:'shortTermDebt', accountType:AccountType.LIABILITIES, closingType:ClosingType.PERMANENT, balanceType:BalanceType.CREDIT,
			cashFlowStatementType:CashFlowStatementType.OPERATING_ACTIVITIES)
		equipment = new Account(name:'equipment', accountType:AccountType.ASSETS, closingType:ClosingType.PERMANENT, balanceType:BalanceType.DEBIT,
			cashFlowStatementType:CashFlowStatementType.OPERATING_ACTIVITIES)
		credit1 = new JournalEntryAmount(account:cash, amount:5.0)	
		credit2 = new JournalEntryAmount(account:shortTermDebt, amount:3.0)
		debit1 = new JournalEntryAmount(account:equipment, amount:8.0)
		je1.credits.add(credit1)
		je1.credits.add(credit2)
		je1.debits.add(debit1)
		d2 = new Date(2000)
		je2 = new JournalEntry(dateTime:d2)
		credit3 = new JournalEntryAmount(account:cash, amount:5.0)	
		credit4 = new JournalEntryAmount(account:shortTermDebt, amount:3.0)
		debit2 = new JournalEntryAmount(account:equipment, amount:8.0)
		je2.credits.add(credit3)
		je2.credits.add(credit4)
		je2.debits.add(debit2)
		j = new Journal()
		j.entries.add(je1)
		j.entries.add(je2)
	}

	@Test
	void addJournalEntry() {
		assert 0 == je1.balance()
		assert 0 == je2.balance()
		assert 2 == j.entries.size
		assert credit1 == je1.findCreditAccountByName('Cash')
		assert [credit1, credit3] == j.entries*.findCreditAccountByName('Cash')
		assert [je1] == j.entries.findAll { d1 == it.dateTime }
		assert [] == j.entries.findAll {d1 > it.dateTime}
		assert [je1] == j.entries.findAll {it.dateTime < d2}
		assert [] == j.entries.findAll {it.dateTime > d1 && it.dateTime < d2}
	}

	@Test
	void testPost() {
		j.entries.each{assert !it.posted}
		j.entries.each {it.post()}
		j.entries.each {assert it.posted}		
	}
}
